package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsVxBatchDAO;
import org.jsola.hr.dto.TransferAccountsVxBatchAddDTO;
import org.jsola.hr.dto.TransferAccountsVxBatchUpdateDTO;
import org.jsola.hr.entity.TransferAccountsVxBatchDO;
import org.jsola.hr.query.TransferAccountsVxBatchQuery;
import org.jsola.hr.service.ITransferAccountsVxBatchService;
import org.jsola.hr.vo.TransferAccountsVxBatchListVO;
import org.jsola.hr.vo.TransferAccountsVxBatchVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账批次(微信)
 *
 * @author zhr
 */
@Service("hrTransferAccountsVxBatchServiceImpl")
public class TransferAccountsVxBatchServiceImpl implements ITransferAccountsVxBatchService {

    @Autowired
    private ITransferAccountsVxBatchDAO transferAccountsVxBatchDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsVxBatchVO save(TransferAccountsVxBatchAddDTO transferAccountsVxBatchAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsVxBatchDO transferAccountsVxBatchDO = transferAccountsVxBatchAddDTO.to(TransferAccountsVxBatchDO.class);
        // 保存
        transferAccountsVxBatchDO = save(transferAccountsVxBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsVxBatchDO.to(TransferAccountsVxBatchVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsVxBatchUpdateDTO transferAccountsVxBatchUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsVxBatchDO transferAccountsVxBatchDO = transferAccountsVxBatchUpdateDTO.to(TransferAccountsVxBatchDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsVxBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsVxBatchIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsVxBatchIds);
    }


    @Override
    public TransferAccountsVxBatchVO selectById(String transferAccountsVxBatchId, String siteId) {
        TransferAccountsVxBatchDO transferAccountsVxBatchDO = selectDOById(transferAccountsVxBatchId, siteId);
        if (transferAccountsVxBatchDO == null) {
            return null;
        }
        return transferAccountsVxBatchDO.to(TransferAccountsVxBatchVO.class);
    }

    @Override
    public List<TransferAccountsVxBatchListVO> select(TransferAccountsVxBatchQuery transferAccountsVxBatchQuery, String siteId) {
        List<TransferAccountsVxBatchDO> transferAccountsVxBatchDOList = selectDO(transferAccountsVxBatchQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsVxBatchDOList)) {
            return transferAccountsVxBatchDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsVxBatchDOList.stream()
                .map(transferAccountsVxBatchDO -> transferAccountsVxBatchDO.to(TransferAccountsVxBatchListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsVxBatchQuery transferAccountsVxBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsVxBatchQuery,siteId);
        return transferAccountsVxBatchDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsVxBatchListVO> selectPage(TransferAccountsVxBatchQuery transferAccountsVxBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsVxBatchQuery,siteId);
        Page<TransferAccountsVxBatchDO> page = transferAccountsVxBatchDAO.selectPageByExample(example,
                transferAccountsVxBatchQuery.getPageNo(),
                transferAccountsVxBatchQuery.getPageSize());

        return page.to(TransferAccountsVxBatchListVO.class);
    }

    @Override
    public TransferAccountsVxBatchDO selectDOById(String transferAccountsVxBatchId, String siteId) {
        return listById(transferAccountsVxBatchId, siteId);
    }

    @Override
    public List<TransferAccountsVxBatchDO> selectDO(TransferAccountsVxBatchQuery transferAccountsVxBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsVxBatchQuery,siteId);
        return transferAccountsVxBatchDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsVxBatchQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsVxBatchQuery transferAccountsVxBatchQuery, String siteId) {
        Example example = new Example(TransferAccountsVxBatchDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsVxBatchQuery.getOrders());
        return example;
    }
}




